/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.tools.CellEditorLocator;
import org.eclipse.uml2.uml.Element;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.diagram.sequence.sedi.edit.manager.LabelDirectEditManager;
import com.cea.papyrus.diagram.sequence.sedi.edit.manager.LabelLocator;
import com.cea.papyrus.diagram.sequence.sedi.edit.policy.LabelDirectEditPolicy;
import com.cea.papyrus.diagram.sequence.sedi.figures.ILabeledFigure;

/**
 * Abstract edit part for models that are direct editable. The edit part installs a
 * DirectEditPolicy that allows the element to be edited in-place. Subclasses may wish to
 * override {@link #performDirectEdit} as well as {@link #getCellEditorLocator()}. The
 * default Direct Edit Policy requires figures to implement
 * {@link com.cea.papyrus.diagram.sequence.sedi.figures.ILabeledFigure} in order to be editable.
 * 
 * @author frankd@ifi.uio.no
 */
public abstract class AbstractDirectEditableEditPart<T extends Element> extends AbstractEditPart<T> {

    public AbstractDirectEditableEditPart(EObject model, ModelManager modelManager) {
        super(model, modelManager);
    }

    @Override
    protected void createEditPolicies() {
        // super.createEditPolicies();
        installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new LabelDirectEditPolicy());
    }

    @Override
    protected void refreshVisuals() {
        refreshVisualsLabel();
        super.refreshVisuals();
    }

    /** Subclasses may override to set a different label. */
    @SuppressWarnings("unchecked")
    protected abstract void refreshVisualsLabel();

    @Override
    public void performRequest(final Request request) {
        if (request.getType() == RequestConstants.REQ_DIRECT_EDIT)
            performDirectEdit(request);
        else
            super.performRequest(request);
    }

    /**
     * Called in response to a REQ_DIRECT_EDIT request. The default implementation uses a
     * LabelDirectEditManager to coordinate the direct edit operation, as well as the the
     * CellEditorLocator returned by {@link #getCellEditorLocation} to position the edit
     * box. Subclasses may override to accommodate special needs.
     */
    protected void performDirectEdit(Request request) {
        new LabelDirectEditManager(this, getCellEditorLocator()).show();
    }

    /** Returns a suitable CellEditorLocation for the edit part. */
    protected CellEditorLocator getCellEditorLocator() {
        return new LabelLocator((ILabeledFigure) getFigure());
    }
}
